
// pages/car/car.js
var {getGoodsList} = require('../../api/goods.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList:[
    ],
    totalData:"",
    goodsListCar:[],
    cId:""
  },
goodsListCarFun(id){
  getGoodsList({cid:id}).then(data=>{
    console.log(data);
    if(data.meta.status == 200){
      let goodslist =data.message.goods;
      let lists = [];
      goodslist.forEach(item=>{
        let{goods_id,goods_name,goods_small_logo,goods_price} = item;
        let data ={
          id:goods_id,
          goodsImage:goods_small_logo,
          goodsName:goods_name,
          goodsPrice:goods_price == 0 ? "暂无报价":goods_price,   
          goodsPel:goods_price+600         
        }
        if(goods_small_logo){
          lists.push(data);
        }
      })
      this.setData({
        goodsListCar:lists
      })
    }
  })
},
  // 加法计算
  add(options){
    console.log("11",options);
    let index = options.target.dataset.index;
    let num = this.data.goodsList[index].num += 1;
    // console.log(goodsList[index].num);
    let key = "goodsList["+index+"].num";
    let obj = {};
    obj[key] = num;
    this.setData(obj);
    this.getTotal();
  },
  //减法
  reduce(options){
    console.log("22",options);
    let index = options.target.dataset.index;
    let num = this.data.goodsList[index].num ;
    let key = "goodsList["+index+"].num";
    let obj = {};
    if(num <= 1){
      obj[key] = 1;
    }else{
      num --;
      obj[key] = num; 
    }
    this.setData(obj)
    this.getTotal();
  },
  // 删除
  del(options){
    console.log("33",options);
    let index = options.currentTarget.dataset.index;
    console.log(index);
     this.data.goodsList.splice(index,1);
      // console.log(goodsLists);
    //更新数据同时更新视图
    this.setData({
      goodsList:this.data.goodsList
    })
    this.getTotal();//计算总价格
  },
  //计算总价格
  getTotal(){
    // 计算总价格
    if(!this.data.goodsList){
      return;
    }
    let goodsList = this.data.goodsList;
    
    let data = goodsList.reduce((total,item)=>{
      total = total + item.num * item.goodsPrice;
      return total;
    },0)
    this.setData({
      totalData:data
    })
  },
  goPay(){
    if(wx.getStorageSync('GoodsCarLists').length == 0){
        this.setData({
            error: '请加入购物车'
        });
        return;
    }
    if( wx.getStorageSync('GoodsCarLists')[0].goodsPrice == 0 ){
        this.setData({
            error: '该商品暂时无法购买'
        });
        return;
    }
    let date=new Date();
    //年
    let year=date.getFullYear();
    //月
    let month=date.getMonth()+1;
    //日
    let day=date.getDate();
    //时
    let hh=date.getHours();
    //分
    let m=date.getMinutes();
    let mm = m < 10? "0" + m : m
    //秒
    let s=date.getSeconds();
    let  ss =s<10?"0"+s:s;
    let rq=year+"-"+month+"-"+day+"-"+hh+":"+mm+":"+ss;

    let whole = [];
    let wholes = wx.getStorageSync('wholes');
    let GoodsCarLists = wx.getStorageSync('GoodsCarLists');//购物车数据
    GoodsCarLists.forEach(element => {
        let {id,goodsName,goodsPrice,goodsImage,num} = element
        let goodswpay={
            flag:1,
            id,
            goodsName,
            goodsPrice,
            goodsImage,
            num,
            rq,
            jude:true,
        }
        whole.push(goodswpay);
    });
    if(wholes){
        for(let i = 0;i <whole.length;i++){
            wholes.push(whole[i]);
            wx.setStorageSync('wholes',wholes)
        }
    }else{
        wx.setStorageSync('wholes',whole);
    }
    
    wx.navigateTo({
      url: '/pages/pay/pay',
    })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var getCatDaat = wx.getStorageSync('GoodsCarLists')
    this.setData({
      goodsList:getCatDaat,
      cId:wx.getStorageSync("cid")
    })
    this.getTotal();//计算总价格
    this.goodsListCarFun(this.data.cId);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    wx.setStorageSync('GoodsCarLists', this.data.goodsList);
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})